<?php
/**
 * 前台主控制器
 */
namespace Home\Controller;
use Think\Controller;

class IndexController extends Controller {
    public function index(){
        $this->display(':index');
    }

    public function register(){
        $username       = I('post.username');
        $password       = I('post.password');
        $password_check = I('post.password_check');
        $email          = I('post.email', 'email');

        if ($username == null || $password == null
            || $password_check == null || $email == null) {
            $this->error('参数错误');
            return;
        }

        if ($password != $password_check) {
            $this->error('两次输入密码不一致');
            return;
        }

        // | id              | int(11) unsigned    | NO   | PRI | NULL                | auto_increment |
        // | username        | varchar(32)         | NO   | UNI |                     |                |
        // | sha_pass_hash   | varchar(40)         | NO   |     |                     |                |
        // | gmlevel         | tinyint(3) unsigned | NO   | MUL | 0                   |                |
        // | sessionkey      | longtext            | YES  |     | NULL                |                |
        // | v               | longtext            | YES  |     | NULL                |                |
        // | s               | longtext            | YES  |     | NULL                |                |
        // | email           | text                | YES  |     | NULL                |                |
        // | joindate        | timestamp           | NO   |     | CURRENT_TIMESTAMP   |                |
        // | last_ip         | varchar(30)         | NO   |     | 0.0.0.0             |                |
        // | failed_logins   | int(11) unsigned    | NO   |     | 0                   |                |
        // | locked          | tinyint(3) unsigned | NO   |     | 0                   |                |
        // | last_login      | timestamp           | NO   |     | 0000-00-00 00:00:00 |                |
        // | active_realm_id | int(11) unsigned    | NO   |     | 0                   |                |
        // | expansion       | tinyint(3) unsigned | NO   |     | 0                   |                |
        // | mutetime        | bigint(40) unsigned | NO   |     | 0                   |                |
        // | locale          | tinyint(3) unsigned | NO   |     | 0                   |                |
        $data = array(
            'id'                => 0,
            'username'          => strtoupper($username),
            'sha_pass_hash'     => sha1(strtoupper($username . ':' . $password)),
            'gmlevel'           => 0,
            'sessionkey'        => '',
            'v'                 => '0',
            's'                 => '0',
            'email'             => $email,
            'last_ip'           => '127.0.0.1',
            'failed_logins'     => 0,
            'locked'            => 0,
            'last_login'        => '0000-00-00 00:00:00',
            'active_realm_id'   => 0,
            'expansion'         => 0,
            'mutetime'          => 0,
            'locale'            => 0
        );

        $model = M('Account');
        if (!$model->data($data)->add()) {
            $this->error($model->getDbError());
        } else {
            $this->success('创建成功');
        }
    }
}